package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.enums.eUnionNoticeType;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.cross.CrossUnionServerGroup;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.*;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenConfigMgr;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenMgr;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_UNION_UPGRADE_CROSS, desc = "商会晋升跨服")
public class CrossUnionUpgradeCrossCmd extends CrossCommand {

    long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossUnionReqContext context = CrossUnionReqContext.initContext(serverId, packet.getPlayerId()).loadUnionMember().loadUnionInfo();
        if(context.getRet() != 0) {
            ret = context.getRet();
            return;
        }

        if(context.getMember().getPosition() != eUnionPosition.Master.getValue()){
            ret = GameErrorCode.E_UNION_HAD_BEEN_CROSS_UNION;
            return;
        }

        int unionLevel = CrossUnionConfigMgr.getUnionLv(context.getUnionInfo().getExperience());
        if(GameConfig.UNION_UP_LIMIT > unionLevel){
            ret = GameErrorCode.E_UNION_LEVEL_NO_ENOUGH;
            return;
        }

        CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroupByServerId(serverId);
        if(serverGroup == null || serverGroup.getState() == 0){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        synchronized (context.getUnionInfo()){

            if(context.getUnionInfo().isCross()){
                ret = GameErrorCode.E_UNION_HAD_BEEN_CROSS_UNION;
                return;
            }

            context.getUnionInfo().setCross(true);

            CrossUnionMgr.sendUnionChangeBroadcast(eUnionSyncType.Normal.getValue(), context.getUnionInfo(), null, null, 0);
        }

        CrossUnionHuaBenConfigMgr.syncConfig(context.getUnionInfo());

        CrossUnionHuaBenMgr.syncHuaBenData(context.getUnionInfo(), context.getMember(), null);

        //公告
        CrossUnionNoticeMgr.addNotice(context.getUserBaseInfo(), context.getUnionInfo().getUnionUid(), eUnionNoticeType.UpgradeCross.getValue(), context.getMember().getUserBaseInfo().getNickName(), context.getMember().getServerId());

        //日志
        int groupId = CrossUnionServerGroupMgr.getGroupIdByServerId(context.getUnionInfo().getServerId());
        CrossUnionLogMgr.addLogUnionUpgradeCross(groupId, context.getUnionInfo().getUnionUid(), context.getUnionInfo().getServerId(), context.getUnionInfo().getMasterUserId());
    }

    @Override
    public void sendData() {
            CrossUnionProto.UnionUpgradeCrossRespMsg.Builder crossRespMsg = CrossUnionProto.UnionUpgradeCrossRespMsg.newBuilder();
            crossRespMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_UNION_UPGRADE_CROSS_FROM_CROSS, crossRespMsg));
    }
}
